<?php

/* Import Subject controller to get list of subjects */
App::import('Controller', 'Subjects');
App::import('class', 'Utils');

class SchedulesController extends AppController {
	//public $helpers = array('Html', 'Form', 'Session', 'Js');
	public $components = array('CenterInfo', 'Subject');
	
	/**
	 * Index, get all subjects
	 */
	function index($subId = NULL){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		$subjects = new SubjectsController; //subject controller to get all subjects
		$subjects->constructClasses();
		
		$this->set('subjects', $subjects->Subject->find('all'));
		$this->set('subid', $subId);
		
		if ($subId != NULL){
			$this->set('schedules', $schedules = $this->schedulesFromSubIdGet($subId));
			$this->set('subject', $subjects->Subject->findBysub_id($subId));
		}
	}
	
	/**
	 * Get all schedules by sub id
	 */
	private function schedulesFromSubIdGet($subId){
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		$schedules = $this->Schedule->findAllBym_sdl_sub_id($subId);
		return $schedules;
	}
	
	/**
	 * Get table of schedules by subject id
	 */
	public function schedulesBySubIdGet($subId){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get Center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		$subjects = new SubjectsController;
		$subjects->constructClasses();
		
		$schedules = $this->schedulesFromSubIdGet($subId);
		
		$this->set('schedules', $schedules);
		$this->set('subject', $subjects->Subject->findBysub_id($subId));
		$this->render('schedule_table', false);
	}
	
	/**
	 * Add schedule for a subject by subject id
	 */
	public function schedule_add(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		$this->set('subjectId', isset($_POST['subjectId']) ? $_POST['subjectId'] : "");
		
		$weekDay = (isset($_POST['weekDay'])) ? $_POST['weekDay'] : 0;
		$startHour = (isset($_POST['startHour'])) ? $_POST['startHour'] : "";
		$endHour = (isset($_POST['endHour'])) ? $_POST['endHour'] : "";
		$startMin = (isset($_POST['startMin'])) ? $_POST['startMin'] : "";
		$endMin = (isset($_POST['endMin'])) ? $_POST['endMin'] : "";
		
		$this->set('weekDay', $weekDay);
		$this->set('startHour', $startHour);
		$this->set('endHour', $endHour);
		$this->set('startMin', $startMin);
		$this->set('endMin', $endMin);
	}
	
	/**
	 * Admin function used to regist schedule
	 */
	public function scheduleRegist(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Main process
		if(!isset($_POST['scheduleId'])){
			$subId = isset($_POST['subjectId']) ? $_POST['subjectId'] : "";
			$weekDay = isset($_POST['weekDay']) ? $_POST['weekDay'] : "";
			$startHour = isset($_POST['startHour']) ? $_POST['startHour'] : "";
			$endHour = isset( $_POST['endHour'] ) ?  $_POST['endHour'] : ""; 
			$startMin = isset($_POST['startMin']) ? $_POST['startMin'] : "";
			$endMin = isset($_POST['endMin']) ? $_POST['endMin'] : "";
			
			if($subId == '' || $weekDay == '' || $startHour == '' || 
				$endHour == '' || $startMin == '' || $endMin == ''){
				$this->redirect(array(
					'controller' => 'Schedules',
					'action' => 'index'
				));
			}
				$this->Schedule->create();
				$dataNeedToSave = array('Schedule' => array('m_sdl_sub_id' => $subId,
						                'm_sdl_week_day' => $weekDay,
						                'm_sdl_start_hour' => $startHour,
						                'm_sdl_end_hour' => $endHour,
										'm_sdl_start_min' => $startMin,
										'm_sdl_end_min' => $endMin));
				
				if($this->Schedule->save($dataNeedToSave)){
					$this->redirect(array('action' => 'index', $_POST['subjectId']));
				}
			
		} else {
			
			$subId = isset($_POST['subjectId']) ? $_POST['subjectId'] : "";
			$weekDay = isset($_POST['weekDay']) ? $_POST['weekDay'] : "";
			$startHour = isset($_POST['startHour']) ? $_POST['startHour'] : "";
			$endHour = isset( $_POST['endHour'] ) ?  $_POST['endHour'] : ""; 
			$startMin = isset($_POST['startMin']) ? $_POST['startMin'] : "";
			$endMin = isset($_POST['endMin']) ? $_POST['endMin'] : "";
			
			if($subId == '' || $weekDay == '' || $startHour == '' || 
				$endHour == '' || $startMin == '' || $endMin == ''){
				$this->redirect(array(
					'controller' => 'Schedules',
					'action' => 'index'
				));
			}
			
			$dataNeedToSave = array('Schedule.m_sdl_sub_id' => $subId,
					                'Schedule.m_sdl_week_day' => "'".$weekDay."'",
					                'Schedule.m_sdl_start_hour' => $startHour,
					                'Schedule.m_sdl_end_hour' => $endHour,
					                'Schedule.m_sdl_start_min' => $startMin,
									'Schedule.m_sdl_end_min' => $endMin);
			
			$saveCondition = array('Schedule.m_sdl_id' => $_POST['scheduleId']);
			
			if ($this->Schedule->updateAll($dataNeedToSave, $saveCondition)) {
				$this->redirect(array('action' => 'index', $_POST['subjectId']));
			}
		}
	}
	
	/**  
	 * Edit subject's schedule, input is subject id 
	 */
	public function schedule_edit() {
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		
		$scheId = isset($_POST['scheduleId']) ? $_POST['scheduleId'] : "";
		
		if($scheId == ""){
			$this->redirect(array(
				'action' => 'index'
			));
		}
		
		$schedule = $this->Schedule->findBym_sdl_id($scheId);
		$this->set('subjectId', isset($_POST['subjectId']) ? $_POST['subjectId'] : "");
		$this->set('scheduleId', isset($_POST['scheduleId']) ? $_POST['scheduleId'] : "");

		$weekDay = (isset($_POST['weekDay'])) ? $_POST['weekDay'] : $schedule['Schedule']['m_sdl_week_day'];
		$startHour = (isset($_POST['startHour'])) ? $_POST['startHour'] : $schedule['Schedule']['m_sdl_start_hour'];
		$endHour = (isset($_POST['endHour'])) ? $_POST['endHour'] : $schedule['Schedule']['m_sdl_end_hour'];
		$startMin = (isset($_POST['startMin'])) ? $_POST['startMin'] : $schedule['Schedule']['m_sdl_start_min'];
		$endMin = (isset($_POST['endMin'])) ? $_POST['endMin'] : $schedule['Schedule']['m_sdl_end_min'];
		
		$this->set('weekDay', $weekDay);
		$this->set('startHour', $startHour);
		$this->set('endHour', $endHour);
		$this->set('startMin', $startMin);
		$this->set('endMin', $endMin);
	}
	
	/**
	 * Delete schedule
	 */
	public function schedule_delete($subject_id, $scheidList=NULL){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		if ($scheidList == NULL)
			return;

		$subIds = explode ("," , $scheidList);
		
		$subjects = new SubjectsController;
		$subjects->constructClasses();
		
		foreach ($subIds as $schedule_id):						
			if ($this->Schedule->deleteAll(array('Schedule.m_sdl_id' => $schedule_id), false)) {
				$schedules = $this->Schedule->findAllBym_sdl_sub_id($subject_id);
				$this->set('schedules', $schedules);
				$this->set('subject', $subjects->Subject->findBysub_id($subject_id));
				$this->render('schedule_table', false);
				}
		endforeach;
	}
	
	/**
	 * Confirm
	 */
	public function scheduleConfirm(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		
		if(isset($_POST['scheduleId']))
			$this->set('scheduleId', $_POST['scheduleId']);
		
		$this->set('subjectId', isset($_POST['subjectId']) ? $_POST['subjectId'] : "");
		$this->set('weekDay', isset($_POST['weekDay']) ? $_POST['weekDay'] : "");
		$this->set('startHour', isset($_POST['startHour']) ? $_POST['startHour'] : "");
		$this->set('endHour', isset($_POST['endHour']) ? $_POST['endHour'] : "");
		$this->set('startMin', isset($_POST['startMin']) ? $_POST['startMin'] : "");
		$this->set('endMin', isset($_POST['endMin']) ? $_POST['endMin'] : "");
	}
		
}
?>